express = require 'express'
router   = express.Router()

child_process = require 'child_process'

Handler = require '../modules/err_handler.js'
errHandler = Handler.errHandler
dataHandler = Handler.dataHandler

Teacher = require '../modules/schema.js'
	.Teacher

Student = require '../modules/schema.js'
	.Student

SC = require '../modules/schema.js'
	.SC

Course = require '../modules/schema.js'
	.Course

crypto = require 'crypto'

router.get '/admin',(req,res)->
	res.render 'admin/index',{title:"admin"}

router.get '/logout',(req,res)->
	req.session.user = null
	req.session.type = null
	req.session.sc = null
	req.session.err = null
	res.redirect '/'

router.post '/admin/addTeacher',(req,res)->
	patt = new RegExp "^t"
	if not  patt.test req.body.tid
		req.session.err = "无法识别的教师id"
		errHandler req,res
	else if not req.body.passwd?
		req.session.err = "请输入密码"
		errHandler req,res
	else if not req.body.tname?
		req.session.err = "请输入教师名称"
		errHandler req,res
	else
		md5 = crypto.createHash 'md5'
		md5.update req.body.passwd
		passwd = md5.digest 'hex'
		teacher = new Teacher {'_id':req.body.tid,'tname':req.body.tname,'passwd':passwd}
		teacher.save (err)->
			if err?
				console.log(err)
			else
				dataHandler res,'log','ok'

router.post '/admin/teacher',(req,res)->
	patt = new RegExp "^t"
	if not  patt.test req.body.tid
		req.session.err = "无法识别的教师id"
		errHandler req,res
	else if not req.body.passwd?
		req.session.err = "请输入密码"
		errHandler req,res
	else if not req.body.tname?
		req.session.err = "请输入教师名称"
		errHandler req,res
	else
		md5 = crypto.createHash 'md5'
		md5.update req.body.passwd
		passwd = md5.digest 'hex'
		teacher = new Teacher {'_id':req.body.tid,'tname':req.body.tname,'passwd':passwd}
		teacher.save (err)->
			if err?
				console.log(err)
			else
				dataHandler res,'log','ok'

router.post '/admin/genKey',(req,res)->
	number = req.body.number
	copy = req.body.copy
	if number/1? and copy/1?
		if number >= 400
			req.session.err = '每份激活码数过大'
			errHandler req,res
		else
			gen = child_process.spawn 'bin/genSeed',['-s',
				number/1,
				copy/1]
			path = ""
			gen.stdout.on 'data',(data)->
				path += data
			gen.on 'exit',(code)->
				if code is 0
					dataHandler res,'get',"path":path
				else
					req.session.err = 'unknow error!'
					errHandler req,res
	else
		req.session.err = "输入错误！"
		errHandler req,res

router.get '/admin/all/:course',(req,res)->
	Course.findOne '_id':req.param 'course'
		.exec (err,course)->
			if err?
				console.log err
			else if not course?
				console.log 'no course'
			else
				Student.find({'$or':[{'sclass':'信息1203'},{'sclass':'信息1202'},{'sclass':'信息1201'},{'sclass':'计科1201'},{'sclass':'计科1202'},{'sclass':'计科1203'},{'sclass':'计科1301'},{'sclass':'计科1302'},]},'_id').exec (err,student)->
					if err?
						console.log err
					else
						scs = []
						((s)->
							sc = new SC 'student':s._id,'course':course._id
							scs.push sc) s for s in student
						SC.create scs,(err)->
							if err?
								console.log err
							else
								console.log 'done'

router.get '/admin/%E4%BF%A1%E6%81%AF.xlsx',(req,res)->
	xlsx = require 'node-xlsx'
	sheet1 = [[1,2,3],[2,3,4],[3,4,5]]
	buffer = xlsx.build [{'name':'sheet1',data:sheet1}]
	res.writeHead "content-type":"text/xlsx"
	res.write buffer
	res.end()
	

module.exports = router
